﻿using UnityEngine;
using System.Collections;

public class SetShadowMatrix : MonoBehaviour {
    public Shader shadowShader;

    private void Awake()
    {
        GetComponent<Camera>().SetReplacementShader(shadowShader, "ShadowRender");
    }

    // Use this for initialization
    void Start () {
        var cam = GetComponent<Camera>();
        Debug.LogError(GetComponent<Camera>().projectionMatrix);
        Debug.LogError(cam.projectionMatrix[14]);
        Debug.LogError(cam.worldToCameraMatrix);
        Debug.LogError(cam.transform.worldToLocalMatrix);
        Debug.LogError(cam.projectionMatrix * cam.worldToCameraMatrix);
        Debug.LogError(cam.projectionMatrix * cam.transform.worldToLocalMatrix);

        var v4 = Vector4.zero;
        var pos1 = (cam.transform.position + cam.transform.forward + cam.transform.right);

        v4.x = pos1.x;
        v4.y = pos1.y;
        v4.z = pos1.z;
        v4.w = 1;

        Debug.LogError(cam.worldToCameraMatrix * v4);
        Debug.LogError(cam.transform.worldToLocalMatrix * v4);

        v4.x = transform.position.x;
        v4.y = transform.position.y;
        v4.z = transform.position.z;
        v4.w = 1;

        var cen = transform.worldToLocalMatrix * v4;
        Debug.LogError(cen);


	}
	
	// Update is called once per frame
	void Update () {
        var cam = GetComponent<Camera>();
        //var viewProj = cam.projectionMatrix * cam.worldToCameraMatrix;
        //var viewProj = cam.projectionMatrix * cam.transform.worldToLocalMatrix;
        //z 轴相反
        var viewProj = cam.projectionMatrix * cam.worldToCameraMatrix;
        //var viewProj = cam.transform.worldToLocalMatrix;
        Shader.SetGlobalMatrix("texViewProj", viewProj);
	
	}
}
